package cn.yitun.chat.yitunservermanagecore.general.manage.index.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.codec.Encryped;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.MenuVo;
import cn.yitun.chat.yitunservercommonmessage.data.user.LoginDto;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.index.service.IndexService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * date 2018-06-04 14:59:44<br>
 * description
 * 
 * @author yitun<br>
 * @since
 */
@RestController
@RequestMapping("/manage")
public class IndexController extends BaseController{

	@Resource
	IndexService indexService;

	@RequestParameter
	@PermissionMapping(name = "登录", perms = "sys:user:info", isIntercept = false)
	@RequestMapping(method = RequestMethod.POST, value = "/index/login")
	@Encryped(decode = true)
	public Object login(
			HttpServletRequest request,
			@JsonDefine LoginDto loginDto) {
		if (YitunStringUtil.isEmpty(loginDto.getAccount()) || YitunStringUtil.isEmpty(loginDto.getPassword()))
			throw new YitunException(ExceptionEnum.LOSE_FILED );

		ResultMessage rm = indexService.login(request, loginDto);
		return rm;
	}

	@RequestParameter
	@PermissionMapping(name = "加载菜单", perms = "sys:index:menu:list")
	@RequestMapping(method = RequestMethod.POST, value = "/index/menuList")
	public Object menuList(JwtUserDto jwtUserDto) {
		Long userId = jwtUserDto.getUserId();
		String token = jwtUserDto.getToken();
		ResultMessage rm = new ResultMessage();
		List<MenuVo> list = indexService.menuList(userId,token);
		sse();
		rm.bodyPut("list", list);
		return rm;
	}

	private void sse(){
		throw new YitunException(ExceptionEnum.LOSE_FILED);
	}

	@GetMapping("/index/logout")
	@PermissionMapping(name = "登出", perms = "sys:user:info")
	public ResultMessage apiLogout(JwtUserDto jwtUserDto) {
		ResultMessage rm = new ResultMessage();
		indexService.apilogout(jwtUserDto);
		return rm;
	}
}
